/*
    绑定关系相关类的测试
*/

#include "../../src/server/binding.hpp"
#include "../../src/common/logger.hpp"
#include "gtest/gtest.h"

mq::BindingManager::BindingManagerPtr binding_manager_ptr;
class BindingTest: public testing::Environment
{
public:
    virtual void SetUp() override
    {
        binding_manager_ptr = std::make_shared<mq::BindingManager>("./data/mate.db");
    }

    virtual void TearDown() override
    {
        // binding_manager_ptr->clear();
    }
};


// 插入测试 & 恢复测试
TEST(binding_test, insert_test)
{
    // binding_manager_ptr->bind("exchanger_1", "msg_queue_1", "news.musics.pop", true);
    // binding_manager_ptr->bind("exchanger_1", "msg_queue_2", "news.musics.classic", true);
    // binding_manager_ptr->bind("exchanger_1", "msg_queue_3", "news.musics.#", true);
    // binding_manager_ptr->bind("exchanger_2", "msg_queue_1", "news.musics.pop", true);
    // binding_manager_ptr->bind("exchanger_2", "msg_queue_2", "news.musics.classic", true);
    // binding_manager_ptr->bind("exchanger_2", "msg_queue_3", "news.musics.#", true);
    ASSERT_EQ(binding_manager_ptr->size(), 6);

    // 重复绑定
    bool ret = binding_manager_ptr->bind("exchanger_2", "msg_queue_3", "news.musics.#", true);
    ASSERT_EQ(ret, true);
    ASSERT_EQ(binding_manager_ptr->size(), 6);
}

// 查找测试
TEST(binding_test, get_test)
{
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_1", "msg_queue_1"), true);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_1", "msg_queue_2"), true);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_1", "msg_queue_3"), true);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_2", "msg_queue_1"), true);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_2", "msg_queue_2"), true);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_2", "msg_queue_3"), true);

    // 获取绑定关系
    mq::Binding::BindingPtr binding_ptr = binding_manager_ptr->getBinding("exchanger_1", "msg_queue_3");
    ASSERT_EQ(binding_ptr->_exchanger_name, "exchanger_1");
    ASSERT_EQ(binding_ptr->_msg_queue_name, "msg_queue_3");
    ASSERT_EQ(binding_ptr->_binding_key, "news.musics.#");
    ASSERT_EQ(binding_ptr->_durable, true);

    // 获取指定交换机的所有绑定关系
    mq::BindingManager::MsgQueueBindingMap msg_queue_binding_map = binding_manager_ptr->getExchangerBindings("exchanger_1");
    ASSERT_EQ(msg_queue_binding_map.size(), 3);
    ASSERT_EQ(msg_queue_binding_map["msg_queue_1"]->_binding_key, "news.musics.pop");
    ASSERT_EQ(msg_queue_binding_map["msg_queue_2"]->_binding_key, "news.musics.classic");
    ASSERT_EQ(msg_queue_binding_map["msg_queue_3"]->_binding_key, "news.musics.#");
}

// 删除测试
TEST(binding_test, delete_test)
{
    // 解除绑定关系
    binding_manager_ptr->unbind("exchanger_1", "msg_queue_1");
    ASSERT_EQ(binding_manager_ptr->size(), 5);
    ASSERT_EQ(binding_manager_ptr->hasBinding("exchanger_1", "msg_queue_1"), false);

    // 删除指定消息队列的所有绑定关系
    binding_manager_ptr->removeMsgQueueBindings("msg_queue_2");
    ASSERT_EQ(binding_manager_ptr->size(), 3);

    // 删除指定交换机的所有绑定关系
    binding_manager_ptr->removeExchangerBindings("exchanger_1");
    ASSERT_EQ(binding_manager_ptr->size(), 2);
    ASSERT_EQ(binding_manager_ptr->getExchangerBindings("exchanger_1").size(), 0);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    return RUN_ALL_TESTS();
}
